package com.song.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.song.pojo.Employee;
import com.song.pojo.User;
import com.song.service.UserService;
import com.song.utils.DataGridViewResult;
import com.song.utils.SystemConstant;
import com.song.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2021/5/29 - the current system date.
 * 20:22 - the current system time.
 */
@RestController
@RequestMapping("/admin/user")
public class UserManagerController {
    @Autowired
    private UserService userService;

    /**
     * 用户信息列表
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(UserVo userVo){
        //设置分页信息
        PageHelper.startPage(userVo.getPage(),userVo.getLimit());

        //调用查询方法
        List<User> userList = userService.findUserList(userVo);

        //创建分页对象
        PageInfo<User> pageInfo = new PageInfo<User>(userList);

        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(User user){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用新增用户的方法
        if(userService.addUser(user)>0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "添加成功");
        }else{
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "添加失败");
        }
        return JSON.toJSONString(map);
    }


    /**
     * 修改用户
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    public String updateUser(User user){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用修改员工的方法
        if(userService.updateUser(user)>0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "添加成功");
        }else{
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "添加失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(int id){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用删除用户的方法
        if(userService.deleteById(id)>0){
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "删除成功");
        }else{
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }
}
